package com.edward.action;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;
import org.springframework.security.annotation.Secured;

import com.edward.JsonDateProcessor;
import com.edward.PagerModel;
import com.edward.model.BVItem;
import com.edward.model.BVList;
import com.edward.service.BVItemManager;
import com.edward.service.BVListManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BVListAction extends ActionSupport{


	private static final long serialVersionUID = 4912812398720905688L;

	private BVItemManager bvItemManager;
	
	private BVListManager bvListManager;
	
	private BVList bvList;
	
	private BVItem bvItem;
	
	private String type = "";
	
	private int limit;
	
	private int start;
	
	public BVList getBvList() {
		return bvList;
	}

	public void setBvList(BVList bvList) {
		this.bvList = bvList;
	}

	public BVItem getBvItem() {
		return bvItem;
	}

	public void setBvItem(BVItem bvItem) {
		this.bvItem = bvItem;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setBvItemManager(BVItemManager bvItemManager) {
		this.bvItemManager = bvItemManager;
	}

	public void setBvListManager(BVListManager bvListManager) {
		this.bvListManager = bvListManager;
	}

	//添加bvList 的表头
	@Secured("ROLE_ORDER_ADD")
	public String add() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		bvList.setTotal(bvList.getOceanFreight());
		bvList.setState("新建");
		bvListManager.addBVList(bvList);
		int total = bvListManager.listBVList().size();
		response.getWriter().print("{success : true , msg : '添加成功!',bvList_id:"+ bvList.getId()+",total:" + total + "}");
		return null;
	}
	
	@Secured("ROLE_ORDER_UPDATE")
	public String update() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		BVList b = bvListManager.getBVList(bvList.getId());
		bvList.setFobChina(b.getFobChina());
		bvList.setTotal(bvList.getTotal() + bvList.getOceanFreight() - b.getOceanFreight());
		bvList.setItems(b.getItems());
		bvListManager.updateBVList(bvList);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_ORDER_UPDATE")
	public String delete() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		BVList s = bvListManager.getBVList(bvList.getId());
		if(s.getItems().size() != 0){
			response.getWriter().print("{success : false , msg : '删除失败，存在BV货物列表，请先删除BV货物!!'}");
		}else{
			bvListManager.deleteBVList(s.getId());
			response.getWriter().print("{success : true , msg : '删除成功!'}");
		}
		return null;
	}

	public String getList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
		
		if(type.equals("")|| type.equals("全部")){
			pager = bvListManager.pagerList(start, limit );
		}else{
			pager = bvListManager.pagerList(start, limit,type );
		}
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	
	
//	//添加bvList 的 货物明细
	@Secured("ROLE_ORDER_ADD")
	public String addItem() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		BVList s = bvListManager.getBVList(bvList.getId());
		bvItem.setAmount(bvItem.getUnitPrice() * bvItem.getQty());
		bvItemManager.addBVItem(bvItem);
		s.setTotal(s.getTotal() + bvItem.getAmount()); 
		s.setFobChina(s.getTotal()-s.getOceanFreight());
		s.getItems().add(bvItem);
		bvListManager.updateBVList(s);
		response.getWriter().print("{success : true , msg : '添加成功!'}");
		return null;
	}
	
	@Secured("ROLE_ORDER_UPDATE")
	public String updateItem() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		BVItem i =  bvItemManager.getBVItem(bvItem.getId());
		bvItem.setAmount(bvItem.getUnitPrice() * bvItem.getQty());
		BVList s = bvListManager.getBVList(bvList.getId());
		s.setTotal(s.getTotal() + bvItem.getAmount() - i.getAmount()); 
		s.setFobChina(s.getTotal()-s.getOceanFreight());
		bvListManager.updateBVList(s);
		bvItemManager.updateBVItem(bvItem);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_ORDER_UPDATE")
	public String deleteItem() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		BVItem i = bvItemManager.getBVItem(bvItem.getId());
		BVList s = bvListManager.getBVList(bvList.getId());
		s.setTotal(s.getTotal() - i.getAmount() ); 
		s.setFobChina(s.getTotal()-s.getOceanFreight());
		bvListManager.updateBVList(s);
		bvItemManager.deleteBVItem(i.getId());
		response.getWriter().print("{success : true , msg : '删除成功'}");
		return null;
	}


	//获取grid 显示 的 分页信息
	public String getItems() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
	    pager = bvItemManager.pagerBVItem(start, limit,bvList.getId());
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	
	public String finish() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		BVList b = bvListManager.getBVList(bvList.getId());
		if(!b.getState().equals("返回")){
			response.getWriter().print("{success:false, msg : '只用在返回状态的形式发票才能设置完结!'}");
			return null;
		}
		b.setState("完结");
		bvListManager.updateBVList(b);
		response.getWriter().print("{success : true , msg : '成功提交!'}");
		return null;
	}
	
	public String turnback() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		BVList b = bvListManager.getBVList(bvList.getId());
		if(!b.getState().equals("新建")){
			response.getWriter().print("{success:false, msg : '只用在新建状态的形式发票才能设置返回!'}");
			return null;
		}
		b.setState("返回");
		b.setNumber(bvList.getNumber());
		bvListManager.updateBVList(b);
		response.getWriter().print("{success : true , msg : '成功提交!'}");
		return null;
	}
	
	@Secured("ROLE_ORDER")
	 public String getListJudge() throws Exception {
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print("{success : true , msg : '成功!',deny:false}");
		 return null;
	 }
	
	

}
